﻿using UnityEngine;
using UnityEngine.Events;

public class CharacterController2D : MonoBehaviour
{

    /*
     * 角色控制器
     */


    public float jumpForce;                          // 弹跳力
    public bool canAirControl = false;               // 在空中时，是否能控制
    public LayerMask groundLayer;                    // 定义哪一个Layer是地面
    public LayerMask boxLayer;
    public Transform GroundCheck;                    // 用于判定地面的空物体

    private bool isOnGrounded;                       // 当前是否在地面上
    bool isOnBox;
    public bool isFacingRight = true;                // 玩家是否面朝右边


    private Rigidbody2D rb2d;
    private Collider2D coll;
    Animator anim;

    //音效组件
    //AudioSource audioSource;
    //public AudioClip jumpSound;

    [Header("Events")]
    [Space]

    public UnityEvent OnLandEvent;

    [System.Serializable]
    public class BoolEvent : UnityEvent<bool> { }

    public bool isGrounded { get; private set; }

    private void Awake()
    {
        rb2d = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        coll = GetComponent<Collider2D>();

        if (OnLandEvent == null)
            OnLandEvent = new UnityEvent();

        //audioSource = GetComponent<AudioSource>();
    }

    private void FixedUpdate()
    {
        bool wasGrounded = isOnGrounded;
        isOnGrounded = false;

        //判断接触地面
        Collider2D[] colliders = Physics2D.OverlapCircleAll(GroundCheck.position, 0.1f, groundLayer);
        for (int i = 0; i < colliders.Length; i++)
        {
            if (colliders[i].gameObject != gameObject)
            {
                isOnGrounded = true;
                if (!wasGrounded)
                    OnLandEvent.Invoke();
            }
        }
    }


    public void Move(float move, bool jump)
    {

        // 玩家在地面时，或者可以空中控制时，才能移动
        if (isOnGrounded || canAirControl)
        {
            // 输入变量move决定横向速度
            rb2d.velocity = new Vector2(move, rb2d.velocity.y);
        }
        if (!isOnGrounded)
        {
            if (move > 0 && isFacingRight)
            {
                rb2d.velocity = new Vector2(Mathf.Max(move, rb2d.velocity.x), rb2d.velocity.y);
            }
            else if (move < 0 && !isFacingRight)
            {
                rb2d.velocity = new Vector2(Mathf.Min(move, rb2d.velocity.x), rb2d.velocity.y);
            }
        }

        // 在地面时按下跳跃键，就会跳跃
        if (isOnGrounded && jump)
        {
            isOnGrounded = false;
            // 施加弹跳力
            rb2d.velocity = new Vector2(move, jumpForce);

            //audioSource.clip = jumpSound;
            //audioSource.Play();
        }

        // 面朝右时按左键，或面朝左时按右键，都会让角色水平翻转
        Flip(move);
    }


    private void Flip(float move)
    {
        if (move > 0 && !isFacingRight || move < 0 && isFacingRight)
        {
            // true变false，false变true
            isFacingRight = !isFacingRight;
            // 缩放的x轴乘以-1，图片就水平翻转了
            transform.localScale = new Vector3(isFacingRight ? 1 : -1, 1, 1);
        }
    }

    private void Update()
    {
        isGrounded = isOnGrounded;
        isOnBox = Physics2D.Raycast(GroundCheck.position, Vector2.down, 0.1f, boxLayer);
        //Debug.DrawRay(GroundCheck.position, Vector2.down * 0.1f,Color.red);
        //Debug.Log("isOnbox"+isOnBox);
        if (isOnBox)
        {
            isOnGrounded = true;
            isGrounded = true;
        }
        //if (coll.IsTouchingLayers(boxLayer))
        //{
        //    isOnGrounded = true;
        //    isGrounded = true;
        //    isOnBox = true;
        //}
        anim.SetBool("isOnGround",isOnGrounded);
    }
}
